﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Text.RegularExpressions;

namespace Transformer.iHunter.Plugins.BBS
{
    public partial class BbsBoardConfigDialog : Form
    {
        private static Regex regex_BoardName = new Regex(@"[\w\s\._]*");

        public BbsBoardConfigDialog()
        {
            InitializeComponent();
        }

        public void SetBoard(string board)
        {
            System.Diagnostics.Debug.Assert(!(board.StartsWith(" ")));

            if (board.Contains("("))
            {
                int ind = board.IndexOf('(');
                textBox_Boards.Text = board.Substring(0,ind).TrimEnd();

                board = board.Substring(ind);

                checkBox_Attach.Checked = board.Contains("Attach");
                checkBox_ThreadMode.Checked = board.Contains("FirstPost");
                checkBox_Sign.Checked = true;
                if (board.Contains("M_only"))
                    radioButton_M.Checked = true;
                else if (board.Contains("G_only"))
                    radioButton_G.Checked = true;
                else if (board.Contains("M_and_G"))
                    radioButton_B.Checked = true;
                else if (board.Contains("M_or_G"))
                    radioButton_MorB.Checked = true;
                else
                    checkBox_Sign.Checked = false;
            }
            else
            {
                textBox_Boards.Text = board;
                checkBox_Attach.Checked = false;
                checkBox_ThreadMode.Checked = false;
                checkBox_Sign.Checked = false;
            }
        }

        public List<string> Boards
        {
            get
            {
                List<string> boards = new List<string>();
                string[] bnames = textBox_Boards.Text.Split(
                    new char[] { ' ', '\t', '\r', '\n' },
                    StringSplitOptions.RemoveEmptyEntries
                    );

                string atti = " (";
                if (checkBox_ThreadMode.Checked)
                    atti += "FirstPost ";
                if (checkBox_Attach.Checked)
                    atti += "Attach ";

                if (checkBox_Sign.Checked)
                {
                    if (radioButton_M.Checked)
                        atti += "M_only";
                    else if (radioButton_G.Checked)
                        atti += "G_only";
                    else if (radioButton_B.Checked)
                        atti += "M_and_G";
                    else if (radioButton_MorB.Checked)
                        atti += "M_or_G";
                }
                atti = atti.TrimEnd() + ")";

                foreach (string b in bnames)
                {
                    string boardname = b.Trim();
                    if (b.Length == 0)
                        continue;

                    if (atti.Length > 3)
                        boardname += atti;

                    boards.Add(boardname);
                }

                return boards;
            }
        }

        private void button_OK_Click(object sender, EventArgs e)
        {
            if (null == textBox_Boards.Text || textBox_Boards.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Please input at least one board name");
                return;
            }

            Match m = regex_BoardName.Match(textBox_Boards.Text);
            if (!(m.Success && m.Groups[0].Length == textBox_Boards.Text.Length))
            {
                MessageBox.Show("Board names can only contain English letters, digits, . and _");
                return;
            }

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void button_Cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void checkBox_Sign_CheckedChanged(object sender, EventArgs e)
        {
            bool sign = checkBox_Sign.Checked;
            radioButton_B.Enabled = radioButton_M.Enabled = radioButton_G.Enabled = radioButton_MorB.Enabled = sign;
        }
    }
}
